package com.yxd.bdmapdemo.base;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Handler;
import android.view.Gravity;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.yxd.bdmapdemo.utils.L;
import com.yxd.bdmapdemo.utils.PermissionUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by dong on 2017/5/9.
 */

public class App extends Application {

    public static Context mContext;
    public static Handler mHandler;
    public static Thread mMainThread;
    public static long mMainThreadId;

    @Override
    public void onCreate() {
        // 程序创建的时候执行
        L.d("onCreate");
        super.onCreate();
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(this);
        registerSDKCheckReciver();

        PermissionUtils.isPermission(this);


        // 创建一些常见的变量

        // 1.上下文
        mContext = getApplicationContext();

        // 2.创建一个handler
        mHandler = new Handler();

        // 3.得到一个主线程id
        mMainThreadId = android.os.Process.myTid();

        // 4.得到主线程
        mMainThread = Thread.currentThread();


        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                                .addInterceptor(new LoggerInterceptor("---------------------------"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)

                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }

    private BroadcastReceiver receiver;

    //注册广播监听百度地图key，网络 权限
    private void registerSDKCheckReciver() {

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                switch (action) {
                    case SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR:
                        showToast("网络错误");
                        break;
                    case SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR:
                        showToast("key验证失败");
                        break;
                    case SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK:
                        showToast("permission check ok");
                        break;
                    case SDKInitializer.SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_CODE:
                        showToast("error_code");
                        break;
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        //监听网络错误
        intentFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
        //监听百度地图sdk的key是否正确
        intentFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
        intentFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK);
        intentFilter.addAction(SDKInitializer.SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_CODE);
        registerReceiver(receiver, intentFilter);
    }

    public void showToast(CharSequence msg) {
        Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


    @Override
    public void onTerminate() {
        // 程序终止的时候执行
        L.d("onTerminate");
        super.onTerminate();
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
    }

    @Override
    public void onLowMemory() {
        // 低内存的时候执行
        L.d("onLowMemory");
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        // 程序在内存清理的时候执行
        L.d("onTrimMemory");
        super.onTrimMemory(level);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        L.d("onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }


}
